<template>
  <div>
    <headerMiddle title="我的关注" />
    <div class="item" v-for="(item,index) in myFollows" :key="index">
      <!-- 这里是每一个关注的框框 v-for 来遍历数据 -->
      <img :src="item.head_img" alt class="avatar" />
      <div class="itemMiddle">
        <div class="name">{{item.nickname}}</div>
        <div class="date">2019-10-04</div>
      </div>
      <!-- 取消关注哪一个用户,就将当前的用户id 传进去 -->
      <div class="btnCancel" @click="cancelFollow(item.id)">取消关注</div>
    </div>
  </div>
</template>

<script>
import headerMiddle from "../components/headerMiddle";
export default {
  components: {
    headerMiddle
  },
  data() {
    return {
      myFollows: []
    };
  },
  methods: {
    loadPage() {
      //   1. 获取数据
      this.$axios({
        url: "/user_follows",
        method: "get"
      }).then(res => {
        console.log(res.data);
        // 要将这个数据放进一个 data 数据里面 进行渲染
        this.myFollows = res.data.data;

        // 不确认所有数据都有头像,所以要向用户中心那样,遍历处理好头像数据
        this.myFollows.forEach(element => {
          if (!element.head_img) {
            element.head_img = "/static/img/xixi.png";
          } else {
            element.head_img = this.$axios.defaults.baseURL + element.head_img;
          }
        });
      });
    },
    cancelFollow(id) {
      //   取消关注需要获取他的 id
      this.$axios({
        url: "/user_unfollow/" + id,
        method: "get"
      }).then(res => {
        console.log(res.data);
        this.loadPage();
      });
    }
  },
  mounted() {
    this.loadPage();
  }
};
</script>

<style lang="less" scoped>
.item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4.167vw;
  border-bottom: 1px solid #d7d7d7;
}
.avatar {
  width: 11.111vw;
  border-radius: 5.556vw;
}
.itemMiddle {
  flex: 1;
  padding-left: 5.556vw;
  .date {
    font-size: 14px;
    color: #dfdfdf;
  }
}
.btnCancel {
  width: 20.278vw;
  height: 8.333vw;
  line-height: 8.333vw;
  color: #959595;
  text-align: center;
  background: #e0e0e0;
  border-radius: 4.167vw;
}
</style>